<?php
require_once 'admin_inc_path.php';

if (isset($_POST['save'])) {

    $response = DAOFactory::getPA1DAO()->queryBySeg($_POST['segment']);
    if ($response):
        foreach ($response as $key => $value):
            $des = DAOFactory::getDescriptionDAO()->load($value->dES);
            $img = DAOFactory::getImageDAO()->load($value->iMAGE);
        endforeach;
    else:
        $des = new Description();
        $img = new Image();
        $p1 = new PA1();
        
     endif;

        $errors = array();
        $imgCont = new FileController();
        if ($imgCont->attach_file($_FILES['image']) && $imgCont->process_image(_IMAGE_, 'index_page')) {
            $img->name = $imgCont->optimized_name;
            $img->path = 'temp';
           
        } else {
            echo __PIMGUP__;
            //print_r($imgCont->errors);
            
        }
    
    $des->textEn = $_POST['description_en'];
    $des->textSv = $_POST['description_sv'];
    $p1->seg = $_POST['segment'];
    

    if ($response) {

        $res1 = DAOFactory::getDescriptionDAO()->update($des);
        $res2= DAOFactory::getImageDAO()->update($img);
        echo __UPDATED__;
        //var_dump ($res1,$res2);
    } else {
        $des_id = DAOFactory::getDescriptionDAO()->insert($des);
        $img_id= DAOFactory::getImageDAO()->insert($img);
        $p1->dES = $des_id;
        $p1->iMAGE = $img_id;
        $response = DAOFactory::getPA1DAO()->insert($p1);
        echo __INSERTED__;
        //var_dump($response);
    }
}

    
//Extremely dirty implementation for getting 5 segments; 
//but unfortunitely do not have enough brain at this mid night; and Linus wants everything so fast..

/*index page data
    $response = DAOFactory::getPA1DAO()->queryBySeg(1);
    if ($response):
        foreach ($response as $key => $value):
            $$des1= DAOFactory::getDescriptionDAO()->load($value->dES);
            $$img1 = DAOFactory::getImageDAO()->load($value->iMAGE);
        endforeach;
     endif;
     
     $response = DAOFactory::getPA1DAO()->queryBySeg(2);
    if ($response):
        foreach ($response as $key => $value):
            $$des2 = DAOFactory::getDescriptionDAO()->load($value->dES);
            $$img2 = DAOFactory::getImageDAO()->load($value->iMAGE);
        endforeach;
     endif;
     
     $response = DAOFactory::getPA1DAO()->queryBySeg(3);
    if ($response):
        foreach ($response as $key => $value):
            $$des3 = DAOFactory::getDescriptionDAO()->load($value->dES);
            $$img3 = DAOFactory::getImageDAO()->load($value->iMAGE);
        endforeach;
     endif;
     
     $response = DAOFactory::getPA1DAO()->queryBySeg(4);
    if ($response):
        foreach ($response as $key => $value):
            $$des4 = DAOFactory::getDescriptionDAO()->load($value->dES);
            $$img4 = DAOFactory::getImageDAO()->load($value->iMAGE);
        endforeach;
     endif;

     
     $response = DAOFactory::getPA1DAO()->queryBySeg(5);
    if ($response):
        foreach ($response as $key => $value):
            $$des5 = DAOFactory::getDescriptionDAO()->load($value->dES);
            $$img5 = DAOFactory::getImageDAO()->load($value->iMAGE);
        endforeach;
     endif;*/


?>

<?php include_once 'inc/admin_header.php'; ?>

        <div class="admin_content_wrapper">
            <h1>Index</h1>

            <?php 
          for($i=1;$i<6;$i++):
                //main menu data
        
           
        $mm = DAOFactory::getMainMenuDAO()->load($i);
        
        $mh = 'mh' . $i;   $l1 = 'l1' . $i;  $l2 = 'l2' . $i;
        $$mh = DAOFactory::getMainHeadingDAO()->load($mm->mH);
        $$l1 = DAOFactory::getUrlLinkDAO()->load($mm->link1);
        $$l2 = DAOFactory::getUrlLinkDAO()->load($mm->link2);
        
        $response = DAOFactory::getPA1DAO()->queryBySeg($i);
        
        $des = 'des'.$i;
        $img = 'img'. $i;
        foreach ($response as $key => $value):
            $$des= DAOFactory::getDescriptionDAO()->load($value->dES);
            $$img = DAOFactory::getImageDAO()->load($value->iMAGE);
        endforeach;
        
                
            ?>
            <h2>Segment <?php echo $i ?> (from left)</h2>

            <form action="" method="POST" enctype="multipart/form-data">
                <div class="admin_segment">
                    <table>
                        <tr>
                            <td>

                            </td>
                            <td>
                                <h3>Svenska</h3>
                            </td>
                            <td>
                                <h3>Engelska</h3>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <h3>Heading:</h3>
                            </td>
                            <td>
                                <input type="text" name="mh_sv" value="<?php echo $mh_en = (isset($$mh->textSv)) ? $$mh->textSv : ''; ?>" disabled="true"/>
                            </td>
                            <td>
                                <input type="text" name="mh_en" value="<?php echo $mh_sv = (isset($$mh)) ? $$mh->textEn : ''; ?>" disabled="true"/>
                            </td>
                        </tr>

                        <tr>
                            <td>
                                <h3>Link Name 1:</h3>
                            </td>
                            <td>
                                <input type="text" name="link1_sv" value="<?php echo $l1_sv = (isset($$l1)) ? $$l1->linkSv : ''; ?>" disabled="true"/>
                            </td>
                            <td>
                                <input type="text" name="link1_en" value="<?php echo $l1_en = (isset($$l1)) ? $$l1->linkEn : ''; ?>" disabled="true"/>
                            </td>
                        </tr>

                        <tr>
                            <td>
                                <h3>Link Name 2:</h3>
                            </td>
                            <td>
                                <input type="text" name="link2_sv" value="<?php echo $l2_sv = (isset($$l2)) ? $$l2->linkSv : ''; ?>" disabled="true" />
                            </td>
                            <td>
                                <input type="text" name="link2_en" value="<?php echo $l2_en = (isset($$l2)) ? $$l2->linkEn : ''; ?>" disabled="true" />
                            </td>
                        </tr>

                        <tr>
                            <td>
                                <h3>Description:</h3>
                            </td>
                            <td>
                                <textarea name="description_sv"><?php echo $des_sv = (isset($$des)) ? $$des->textSv : ''; ?> </textarea>
                            </td>
                            <td>
                                <textarea name="description_en"><?php echo $des_en = (isset($$des)) ? $$des->textEn : ''; ?> </textarea>
                            </td>
                        </tr>

                        <tr>
                            <td>
                                <h3>Image:</h3>
                            </td>
                            <td>
                                <input type="file" name="image" />
                            </td>
                            <td>
                                <?php echo $image = (isset($$img)) ? $$img->name : ''; ?>

                            </td>
                        </tr>

                    </table>
                    <input type="hidden" name="segment" value="<?php echo $i; ?>" />
                    <input type="submit" name="save" value="Save and Upload" />
                </div>
            </form>

           <?php
            endfor;
          ?>

        </div>

<?php include_once('inc/admin_footer.php'); ?>